package com.baizhi.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.entity.AdminRole;
import com.baizhi.entity.Menu;
import com.baizhi.entity.Role;
import com.baizhi.entity.RoleResource;
import com.baizhi.service.IAdminRoleService;
import com.baizhi.service.IRoleResourceService;
import com.baizhi.service.IRoleService;
import com.baizhi.util.JwtUtils;
import com.baizhi.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
public class AdminRoleController {
    @Autowired
    private IAdminRoleService iAdminRoleService;
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IRoleResourceService iRoleResourceService;
    @PostMapping("/grantRidForUserId")
    public ResultEntity<String> grantRidForUserId(int[] roleIds,@RequestHeader("Authorization") String token){
        //创建adminRole对象
        AdminRole adminRole = new AdminRole();
        //获取用户id
        DecodedJWT id = JwtUtils.verify(token);
        System.err.println(id);
        Integer userid = Integer.valueOf(id.getClaim("id").asString());
        System.err.println(userid);
        //循环出要添加的角色id
        for (int i=0;i<roleIds.length;i++){
            adminRole.setRoleId(roleIds[i]);
            adminRole.setAdminId(userid);
            iAdminRoleService.addRole(adminRole);
            System.err.println(roleIds[i]);
        }
        return ResultEntity.ok();
    }
    @GetMapping("selectAll")
    public ResultEntity<List<Role>> selectAll() {
        List<Role> roles = iRoleService.selectAllRole();
        if (roles!=null){
            return ResultEntity.ok(roles);
        }else {
            return ResultEntity.error();
        }
    }
    @PostMapping("add")
    public ResultEntity<String> addRole(Role role) {
        Integer integer = iRoleService.addRole(role);
        if (integer==1){
            return ResultEntity.ok();
        }else {
            return ResultEntity.error();
        }
    }
    //删除角色
    @DeleteMapping("delete/{roleId}")
    public ResultEntity<String> deleteRole(@PathVariable("roleId") Integer roleId){
        Integer integer = iRoleService.deleteRole(roleId);
        if (integer==1){
            return ResultEntity.ok();
        }else {
            return ResultEntity.error();
        }
    }
    //根据角色标记查询角色信息
    @GetMapping("selectOne/{roleId}")
    public ResultEntity<Role> selectOne(@PathVariable("roleId")Integer roleId){
        Role role = iRoleService.selectRoleByroleId(roleId);
        if (role!=null){
            return ResultEntity.ok(role);
        }else {
            return ResultEntity.error();
        }
    }
    //修改角色信息
    @PutMapping("update")
    public  ResultEntity<int[]> updateRole(Role role){
        Integer integer = iRoleService.updateRole(role);
        if (integer==1){
            return ResultEntity.ok(new int[0]);
        }else {
            return ResultEntity.error();
        }
    }
    //给角色赋权
    @PostMapping("grantResource")
    public  ResultEntity<String> grantResource(Integer roleId,int[] resourceIds){
        RoleResource roleResource = new RoleResource();
        Integer integer = 0;
        for (int i=0;i<resourceIds.length;i++){
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceIds[i]);
            integer = iRoleResourceService.addRoleResource(roleResource);
        }
        if (integer==1){
            return ResultEntity.ok();
        }else {
            return ResultEntity.error();
        }
    }
    //获取某个角色的权限id
    @GetMapping("selectResourcesByRoleId/{roleId}")
    public  ResultEntity<int[]> selectResourcesByRoleId(@PathVariable("roleId")int roleId){
        List<RoleResource> roleResources = iRoleResourceService.slectRoleResource(roleId);
        int[] integerAry = new int[10];
        int count = 0;
        for (RoleResource roleResource : roleResources) {
            int i = roleResource.getResourceId();
            if (integerAry.length == count) integerAry = Arrays.copyOf(integerAry, count * 2);
            integerAry[count++] = i;
        }
        integerAry = Arrays.copyOfRange(integerAry, 0, count);
        if (roleResources!=null){
            return ResultEntity.ok(integerAry);
        }else {
            return ResultEntity.error();
        }
    }
}
